package sys.spvisor.console.controller;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import sys.spvisor.core.entity.project.TQuestion;
import sys.spvisor.core.service.project.ProjectService;
import sys.spvisor.core.service.project.QuestionService;

@Controller
@RequestMapping("/question")
public class QuestionController {
	
	@Autowired
	QuestionService questionService;
	@Autowired
	ProjectService projectService;
	
	@RequestMapping("/addQuestion.ajax")
	public @ResponseBody Map<String,Object> addQuestion(@Param("proId") int proId,TQuestion record,HttpSession session) {
		String username = (String) session.getAttribute("USER_NAME");
		Long userId =  (Long) session.getAttribute("USER_ID");
		record.setUserId(userId.intValue());
		record.setUserName(username);
		record.setPostTime(new Date(new java.util.Date().getTime()));
		record.setProId(proId);
		record.setProName(projectService.getProjectNameBymsgId(proId));
		Map<String,Object> result = new HashMap<String,Object>();
		int rows = questionService.addQuetion(record);
		if(rows == 1) {
			result.put("message", "添加问题成功");
		} else {
			result.put("message", "添加问题失败");
		}
		return result;
	}
	
	@RequestMapping(value="/findQuestionsByProId.ajax", method = { RequestMethod.POST }) 
	public @ResponseBody Map<String,Object> findQuestionsByProId(int proId) {
		Map<String,Object> result = new HashMap<String,Object>();
		List<TQuestion> lists = questionService.findQuestionsByProId(proId);
		result.put("size", lists.size());
		result.put("lists", lists);
		return result;
	}

}
